package com.winit.openapi.wh.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.ReservationSendWhOrderService;
import com.winit.oms.spi.inbound.command.CancelReservationSendWhOrderCommand;
import com.winit.oms.spi.inbound.command.CreateReservationSendWhOrderCommand;
import com.winit.oms.spi.inbound.command.QueryReservationOrderCommand;
import com.winit.oms.spi.inbound.command.UpdateReservationSendWhOrderCommand;
import com.winit.oms.spi.inbound.vo.ReservationSendWhOrderVo;
import com.winit.oms.spi.inbound.vo.WaitForReservationOrderVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.builder.ReservationSendWhBuilder;
import com.winit.openapi.wh.validator.ReservationSendWhValidator;

/**
 * 预约送仓单
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年12月1日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/reservationSendWh")
public class ReservationSendWhController extends BaseController {
    
    @Resource
    ReservationSendWhOrderService reservationSendWhOrderService;
    /**
     * 查询待预约的直发入库单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/waitingReservationList")
    public String waitingReservationList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryReservationOrderCommand command=new QueryReservationOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ReservationSendWhValidator.validateWaittingReservationOrder(json);
            command.setDestinationWhCode(json.getString("destinationWhCode"));
            command.setOrderNo(json.getString("orderNo"));
            command.setCustomerCode(json.getString("customerCode"));
            command.setSource(json.getString("source"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        List<WaitForReservationOrderVo> volist= reservationSendWhOrderService.getWaitingReservationList(command);
        if(volist!=null&&volist.size()>0)
        {
            List<Map<String, Object>> maps=ReservationSendWhBuilder.buildWaitingReservationList(volist);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("orderList", maps);
            responseMsg.setData(resultMap);
        }
        return SUCCESS;
    }
    
    /**
     * 创建预约单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/createReservationOrder")
    public String createReservationOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        CreateReservationSendWhOrderCommand command=new CreateReservationSendWhOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ReservationSendWhValidator.validateCreateReservationOrder(json);
            ReservationSendWhOrderVo vo= ReservationSendWhBuilder.buildCreateReservationOrder(json);
            command.setReservationSendWhOrderVo(vo);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        String reorderNo=reservationSendWhOrderService.createReservationSendWhOrder(command);
        if(StringUtils.isNotEmpty(reorderNo))
        {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("reservationOrderNo", reorderNo);
            responseMsg.setData(resultMap);
            responseMsg.setMsg("SUCCESS");
        }
        return SUCCESS;
    }
    
    /**
     * 取消预约单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/cancelReservationOrder")
    public String cancelReservationOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        CancelReservationSendWhOrderCommand command=new CancelReservationSendWhOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validMaxLength(json, "reservationOrderNo", 60);
            ValidateUtil.validNotNull(json, "reservationOrderNo");
            command.setReservationOrderNo(json.getString("reservationOrderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        reservationSendWhOrderService.cancelReservationSendWhOrder(command);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }
    
    /**
     * 编辑预约单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/editReservationOrder")
    public String editReservationOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        UpdateReservationSendWhOrderCommand command=new UpdateReservationSendWhOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ReservationSendWhValidator.validateUpdateReservationOrder(json);
            ReservationSendWhOrderVo vo= ReservationSendWhBuilder.buildUpdateReservationOrder(json);
            command.setReservationSendWhOrderVo(vo);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        reservationSendWhOrderService.updateReservationSendWhOrder(command);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }
    
    /**
     * 查询预约单详情接口
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/reservationOrderDetail")
    public String reservationOrderDetail(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryReservationOrderCommand command=new QueryReservationOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "source");
            ValidateUtil.validMaxLength(json, "source", 10);
            String source=json.getString("source");
            command.setSource(source);
            if("S".equals(source))
            {
                ValidateUtil.validNotNull(json, "orderNo");
                ValidateUtil.validMaxLength(json, "orderNo", 64);
                command.setOrderNo(json.getString("orderNo"));
            }else{
                ValidateUtil.validNotNull(json, "reservationOrderNo");
                ValidateUtil.validMaxLength(json, "reservationOrderNo", 60);
                command.setReservationOrderNo(json.getString("reservationOrderNo"));
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ReservationSendWhOrderVo vo=reservationSendWhOrderService.getReservationOrderDetail(command);
        if(vo!=null)
        {
            Map<String, Object> resultMap= ReservationSendWhBuilder.buildReservationOrderDetail(vo);
            responseMsg.setData(resultMap);
        }
        return SUCCESS;
    }
    
    /**
     * 查询货运代理预约送仓单列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/reservationOrderList")
    public String reservationOrderList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryReservationOrderCommand command=new QueryReservationOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 64);
            command.setOrderNo(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ReservationSendWhOrderVo vo=reservationSendWhOrderService.getReservationOrderList(command);
        if(vo!=null)
        {
            Map<String, Object> resultMap= ReservationSendWhBuilder.buildReservationOrderList(vo);
            responseMsg.setData(resultMap);
        }
        return SUCCESS;
    }
    
    /**
     * 打印预约单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/printReservationOrder")
    public String printReservationOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryReservationOrderCommand command=new QueryReservationOrderCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "reservationOrderNo");
            ValidateUtil.validMaxLength(json, "reservationOrderNo", 60);
            command.setReservationOrderNo(json.getString("reservationOrderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        String files=reservationSendWhOrderService.printReservationSendWhOrder(command);
        Map<String, Object> dataMap=new HashMap<String, Object>();
        if (files!=null && files.length()>0) {
            dataMap.put("files", files);
        } 
        responseMsg.setData(dataMap);
        return SUCCESS;
    }
}
